package com.easy.modules.sysmgr.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.handlers.MapHandler;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.easy.common.dao.DBUtilsHelper;
import com.easy.common.dao.IBaseDao;
import com.easy.common.util.BeanUtils;
import com.easy.common.web.page.DataGrid;
import com.easy.common.web.page.Msg;
import com.easy.common.web.page.Pager;
import com.easy.modules.sysmgr.entity.LoginLogEntity;
import com.easy.modules.sysmgr.service.LoginLogServiceI;
import com.easy.modules.sysmgr.web.form.LoginLogForm;

@Service
@Transactional
public class LoginLogServiceImpl implements LoginLogServiceI {

	@Autowired
	private IBaseDao<LoginLogEntity> basedaoLoginLog;
	
	@Autowired
	private DBUtilsHelper dbUtil ;
	
	@Override
	public Msg save(LoginLogForm form) {
		try {
			LoginLogEntity entity = new LoginLogEntity() ;
			
			BeanUtils.copyNotNullProperties(form, entity) ;
			
			this.basedaoLoginLog.add(entity) ;
		} catch (BeansException e) {
			return new Msg(false, "发生错误！") ;
		}
		return new Msg(false, "编辑失败！") ;
	}

	@Override
	public Msg delete(LoginLogForm form) {
		try {
			if(null != form.getIds() && !"".equals(form.getIds())) {
				String[] ids = form.getIds().split(",") ;
				for (String id : ids) {
					this.basedaoLoginLog.delete(LoginLogEntity.class, id) ;
				}
				return new Msg(true, "删除成功！") ;
			} else {
				return new Msg(false, "删除失败！") ; 
			}
		} catch (BeansException e) {
			return new Msg(false, "删除失败！") ;
		}
	}
	
	@Override
	public LoginLogForm get(LoginLogForm form) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = "select t from LoginLogEntity t where 1=1";
		hql = addWhere(hql, form, params);
		
		LoginLogEntity entity = (LoginLogEntity) this.basedaoLoginLog.queryObject(hql, params) ;
		if(null != entity) {
			LoginLogForm pform = new LoginLogForm();
			BeanUtils.copyProperties(entity, pform);
			
			return pform;
		} else {
			return null ;
		}
	}
	
	@Override
	public List<Long> loginTimeChart() {
		List<Long> l = new ArrayList<Long>();
		try {
			int k = 0;
			for (int i = 0; i < 12; i++) {
				String sql = "select count(*) as c from easy_sysmgr_login_log t where HOUR(t.loginTime)>="+k+" and HOUR(t.loginTime)<"+(k+2) ;
				Map<String, Object> query = this.dbUtil.getRunner().query(sql, new MapHandler()) ;
				l.add(Long.parseLong(query.get("c").toString())) ;
				k = k + 2;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return l ;
	}

	@Override
	public DataGrid datagrid(LoginLogForm form) {
		
		List<LoginLogForm> forms = new ArrayList<LoginLogForm>();
		Pager<LoginLogEntity> pager = this.find(form) ;
		if (null != pager && !pager.getDataRows().isEmpty()) {
			for (LoginLogEntity i : pager.getDataRows()) {
				LoginLogForm uf = new LoginLogForm();
				BeanUtils.copyProperties(i, uf);
				forms.add(uf);
			}
		}
		
		DataGrid datagrid = new DataGrid();
		datagrid.setTotal(pager.getTotal());
		datagrid.setRows(forms);
		return datagrid;
	}


	private Pager<LoginLogEntity> find(LoginLogForm form) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = "select t from LoginLogEntity t where 1=1";
		hql = addWhere(hql, form, params);
		return this.basedaoLoginLog.find(hql, params);
	}

	private String addWhere(String hql, LoginLogForm form, Map<String, Object> params) {
		if (form.getLoginAccount() != null && !"".equals(form.getLoginAccount())) {
			hql += " and t.loginAccount=:loginAccount";
			params.put("loginAccount", form.getLoginAccount());
		}
		if (form.getTruename() != null && !"".equals(form.getTruename())) {
			hql += " and t.truename=:truename";
			params.put("truename", form.getTruename());
		}
		return hql;
	}
}
